package com.bjyhl.medical.main.news.detail;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjyhl.core.fragments.LatteFragment;
import com.bjyhl.core.net.RestClient;
import com.bjyhl.core.net.callback.ISuccess;
import com.bjyhl.medical.R;
import com.blankj.utilcode.util.LogUtils;
import com.joanzapata.iconify.widget.IconTextView;

/**
 * Created by Natalya on 2019/1/27 18:22
 * Note: 主页fragment
 */

public class NewsDetailFragment extends LatteFragment implements View.OnClickListener {

    public static final String ARG_MEWS_ID = "MEWS_ID";
    private int mNewsId = -1;
    private TextView tvTitle;
    private WebView wvContent;
    private IconTextView iconTextView;

    public static NewsDetailFragment create(int newsId){
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_MEWS_ID, newsId);
        NewsDetailFragment fragment = new NewsDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mNewsId = bundle.getInt(ARG_MEWS_ID);
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_news_detail;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        tvTitle = $(R.id.tv_title);
        wvContent = $(R.id.wv_content);
        iconTextView = $(R.id.icon_news_back);
        iconTextView.setOnClickListener(this);
        initData();
    }

    private void initData() {
        RestClient.builder()
                .url("http://192.168.8.101:3000/v1/news/getNews/" + mNewsId)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        LogUtils.json("商品详情", response);
                        final JSONObject data =
                                JSON.parseObject(response).getJSONObject("data");
                        String title = data.getString("title");
                        String base_url = data.getString("base_url");
                        String content = data.getString("content");
                        tvTitle.setText(title);
                        wvContent.loadDataWithBaseURL(base_url, content, "text/html", "utf-8", null);
                    }
                })
                .build()
                .get();

    }

    @Override
    public void onClick(View v) {
        pop();
    }
}
